<?php

function hapus_transaksi_from_id($id, $session) {
    mysql_query("DELETE FROM tr_t WHERE trt_id='$id' AND trt_session='$session'");
    header('location:' . URLHOST . 'transaksi');
}

function hapus_transaksi_temp_kemarin() {
    //date_default_timezone_set('UTC');
    $kemarin = date('Y-m-d H:i:s', mktime(date('H'), date('i'), date('s'), date('m'), date('d') - 1, date('Y')));
    mysql_query("DELETE FROM tr_t WHERE trt_waktu < '$kemarin'");
}

function update_transaksi_kadaluarsa() {
    date_default_timezone_set('UTC');
    $seminggu = date('Y-m-d H:i:s', mktime(date('H'), date('i'), date('s'), date('m'), date('d') - 7, date('Y')));
    mysql_query("UPDATE tr_x SET trx_status='5' WHERE trx_status='1' AND trx_waktu < '$seminggu'");
}

function get_transaksi_id_from_session($id) {
    $sql = "SELECT trx_id FROM tr_x WHERE trx_session='$id' ORDER BY trx_id DESC LIMIT 1";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    return $baris['trx_id'];
}

function get_jumlah_keranjang($id, $sess) {
    $keranjang = array();
    $sql = "SELECT * FROM tr_t WHERE trt_trx_id='$id' AND trt_session='$sess' ";
    $hasil = mysql_query($sql);

    while ($row = mysql_fetch_array($hasil)) {
        $keranjang[] = $row;
    }

    return $keranjang;
}

function create_kode_unik($panjang) {
    $str = "";
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $size = strlen($chars);
    for ($i = 0; $i < $panjang; $i++) {
        $str .= $chars[rand(0, $size - 1)];
    }

    return $str;
}

function create_angka_unik() {
    return sprintf('%03d', rand(1, 999));
}

function get_status_transaksi($kode) {
    $ret = "";
    switch ($kode) {
        case '1' :
            $ret = '<span style="color:red">BELUM LUNAS</span>';
            break;
        case '2' :
            $ret = '<span style="color:green">LUNAS</span>';
            break;
        case '3' :
            $ret = '<span style="color:blue">PACKING</span>';
            break;
        case '4' :
            $ret = '<span style="color:blue">TERKIRIM</span>';
            break;
        case '5' :
            $ret = '<span style="color:red">KADALUARSA</span>';
            break;
    }

    return $ret;
}

function get_harga_ongkir($id, $berat) {
    $sql = "SELECT ongkir_harga FROM ongkirs WHERE ongkir_id='$id' LIMIT 1";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    $harga_jne = $baris['ongkir_harga'];
    $beratnya = ceil($berat / 1000);

    return $harga_jne * $beratnya;
}

function get_kode_transaksi($kode) {
    $sql_trx = "SELECT * FROM tr_x WHERE trx_kodeunik='$kode' ";
    $hasil_trx = mysql_query($sql_trx);
    $baris_trx = mysql_num_rows($hasil_trx);

    if ($baris_trx < 1) {
        //echo "\n <p style=\"padding:5px; text-align:center;\">Kode transaksi tidak ditemukan</p>";
    } else {
        if ($baris_trx == 1) {
            //$trx_id = mysql_result($hasil_trx, "0", "trx_id");
            $trx_kodeunik = mysql_result($hasil_trx, "0", "trx_kodeunik");
            $trx_angkaunik = mysql_result($hasil_trx, "0", "trx_angkaunik");
            $trx_waktu = tgl_indo(mysql_result($hasil_trx, "0", "trx_waktu"));
            $trx_total = format_rupiah(mysql_result($hasil_trx, "0", "trx_total"));
            $trx_status = get_status_transaksi(mysql_result($hasil_trx, "0", "trx_status"));
            //$trx_ongkir = mysql_result($hasil_trx, "0", "trx_ongkir");
            $trx_nama = mysql_result($hasil_trx, "0", "trx_nama");
            //$trx_alamat = mysql_result($hasil_trx, "0", "trx_alamat");
            $trx_notelp = mysql_result($hasil_trx, "0", "trx_notelp");
            //$trx_email = mysql_result($hasil_trx, "0", "trx_email");
            $trx_ongkir_id = mysql_result($hasil_trx, "0", "trx_ongkir_id");
            $trx_kota = get_ongkir_kota_from_id($trx_ongkir_id);
            $resi = mysql_result($hasil_trx, "0", "trx_noresi");
            $trx_noresi = ($resi == null) ? '-' : $resi;
        }

        echo '<script type="text/javascript">';
        //echo ' <!--';
        echo ' function popup() {';
        echo ' window.open( "' . URLEMAIL . $trx_kodeunik . '", "myWindow", ';
        echo ' \'height=600,width=700,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=no,titlebar=no\' )';
        echo ' }';
        //echo ' //-->';
        echo ' </script>';

        echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
        echo "<tr><th>Kode</th><th>Nama</th><th>Kontak</th><th>Kota</th><th>Tanggal</th><th>Total</th><th>Kode Unik</th><th>Status</th><th>No Resi</th><th></th></tr>";
        echo "<tr><td><strong>$trx_kodeunik</strong></td><td>$trx_nama</td><td>$trx_notelp</td><td>$trx_kota</td><td>$trx_waktu</td><td>Rp $trx_total</td><td>$trx_angkaunik</td><td><strong>$trx_status</strong></td><td>$trx_noresi</td><td><a href='#' onClick='popup()'>Detail</a></td></tr>";
        echo "</table>";
    }
}
?>
